﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows.Forms;
using WindowClient.MessageService;
using WindowClient.UserService;
using Message = WindowClient.UserService.Message;
using User = WindowClient.UserService.User;

namespace WindowClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int id = 0;
            if (Int32.TryParse(textBox1.Text, out id))
            {
                UserService.UserServiceClient c = new UserServiceClient();
                var user = c.GetById(id);
                if (user != null)
                    listBox1.Items.Add(String.Format("[{0}] [{1} {2}] [{3}] [{4}]", user.Id, user.FirstName, user.LastName, user.Login, user.Email));
                else
                    listBox1.Items.Add("Service can't process request");
            }
            else
            {
                listBox1.Items.Add("Parse error!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            UserService.UserServiceClient c = new UserServiceClient();
            User u = new User()
                         {
                             FirstName = textBox2.Text,
                             LastName = textBox3.Text,
                             Login = textBox4.Text,
                             Email = textBox5.Text,
                             Password = "a",
                         };
            try
            {
                c.Add(u);
                listBox1.Items.Add("User added");
            }
            catch (FaultException ex)
            {
                listBox1.Items.Add("Service can't process request");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            UserService.UserServiceClient c = new UserServiceClient();
            var user = c.GetByLogin(textBox6.Text);
            if (user != null)
                listBox1.Items.Add(String.Format("[{0}] [{1} {2}] [{3}] [{4}]", user.Id, user.FirstName, user.LastName, user.Login, user.Email));
            else
                listBox1.Items.Add("Service can't process request");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int messageId = 0;
            if (Int32.TryParse(textBox1.Text, out messageId))
            {
                UserService.UserServiceClient c = new UserServiceClient();
                var user = c.GetByMessageId(messageId);
                if (user != null)
                    listBox1.Items.Add(String.Format("[{0}] [{1} {2}] [{3}] [{4}]", user.Id, user.FirstName, user.LastName, user.Login, user.Email));
                else
                    listBox1.Items.Add("Service can't process request");
            }
            else
            {
                listBox1.Items.Add("Parse error!");
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            MessageService.MessageServiceClient c = new MessageServiceClient();
            MessageService.Message m = new MessageService.Message
                                           {
                                               Content = textBox8.Text,
                                               DateTime = DateTime.Now,
                                               Uri = textBox9.Text,
                                           };
            try
            {
                c.SendMessage(m);
                listBox2.Items.Add("Message added");
            }
            catch (Exception)
            {
                listBox2.Items.Add("Service can't process request");
            }
        }


        private void button6_Click(object sender, EventArgs e)
        {
            MessageService.MessageServiceClient c = new MessageServiceClient();
            var m =c.GetLastMessage(new Uri(textBox10.Text));

            listBox2.Items.Add(String.Format("[{0}] [{1}] [{2}] {3}", m.DateTime, m.Uri, m.User!=null?m.User.Login:"", m.Content));
        }
    }
}
